<?php
namespace Admin\Controller;
use Think\Controller;
class GoodsController extends Controller {
    protected $data =[];
    protected $model = null;
    protected $_validate = [
        // array(验证字段,验证规则,错误提示,[验证条件,附加规则,验证时间])
        array('goods_name','unique','商品名不能为空'),
        array('shop_price','length','请输入价格','1,11'),
        array('ori_img'),
        array('goods_weight'),
        array('goods_number'),
        ];

    public function __construct(){
        parent::__construct();

        $this->model = D('Goods');
    }

    public function msg($msg){
        echo "<script>alert('数据上传".$msg."')</script>";
    }
    
    // public function upload(){
    // }

    // 添加goodsadd方法
    public function goodsadd(){
        if(!IS_POST){
            $cates = D('cate');
            $catelist = $cates->getTree();
            $this->assign('catelist',$catelist);
            // var_dump($catelist);

            $this->display();
        } else {
            // 表单获得的数据上传
            // $GoodsModel = D('Goods');
            if(!$this->model->create()){
                echo $this->model->getError();
            }else{
                if(isset($_FILES['goods_img'])){
                    $upload = new \Think\Upload();// 实例化上传类
                    $upload->maxSize   =     3145728 ;// 设置附件上传大小
                    $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
                    $upload->rootPath  =      './Public/images/Uploads/'; // 设置附件上传根目录
                    $upload->savePath  =      ''; // 设置附件上传（子）目录
                    // 上传文件 
                    $info   =   $upload->upload();
                    // var_dump($info);
                    // 上传错误提示错误信息    
                    if(!$info) {
                        $this->model->error($upload->getError());
                    }else{// 上传成功 获取上传文件信息
                        foreach($info as $file){
                            $this->model->ori_img = $data['goods_img'] . $file['savepath'].$file['savename'];//获取原始大小图片的路径和名字,赋值给ori_img
                            $this->model->goods_img = $this->model->ori_img.'208X208.png';
                            $this->model->thumb_img = $this->model->ori_img.'100X100.png';
                            var_dump($this->model->goods_img);

                            // var_dump($data);exit;
                            // 缩略图生成
                            $image = new \Think\Image();
                            $image = $image->open('./Public/images/Uploads/' . $this->model->ori_img);

                            $image->thumb(208,208,\Think\Image::IMAGE_THUMB_FILLED)->save($upload->rootPath . $this->model->goods_img);
                            $image->thumb(100,100,\Think\Image::IMAGE_THUMB_FILLED)->save($upload->rootPath . $this->model->thumb_img);
                        }
                    }
                    // 上传文件
                    $this->model->add()?$this->msg('成功'):$this->msg('失败');
                    // var_dump($this->model);
                }
            }
        }
    }

    // public function goodsadd(){
    //     if(!IS_POST){
    //         $this->display();
    //     }else{
    //         // // 文件上传
    //         // $upload = new \Think\Upload();// 实例化上传类
    //         // $upload->maxSize   =     3145728 ;// 设置附件上传大小
    //         // $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
    //         // $upload->rootPath  =      './Uploads/'; // 设置附件上传根目录
    //         // $upload->savePath  =      ''; // 设置附件上传（子）目录
    //         // // 上传文件 
    //         // $info   =   $upload->upload();
    //         // // 上传错误提示错误信息    
    //         // if(!$info) {
    //         //     $this->error($upload->getError());
    //         // }else{// 上传成功 获取上传文件信息
    //         //     foreach($info as $file){
    //         //         $this->data['goods_img'] = $file['savepath'].$file['savename'];
    //         //     }
    //         // }

    //         // 表单数据上传
    //         $addgoods = D('Goods');
    //         $addgoods->add($_POST)?$this->msg('成功'):$this->msg('失败');
    //         // $addgoods->add($_POST);
    //         // var_dump($_POST);
    //         $this->redirect('Admin/Goods/goodsadd');
    //     }
    // }

    // 添加goodslist方法
    public function goodslist(){
    	$cates = D('cate');
        //分页
        $count  = $this->model->count('*');         //查询总的记录条数
        $Page  = new \Think\Page($count,10);     //实例化分页类,传入总记录条数和每页显示条数
        $show   = $Page->show();                //分页显示输出
        // 进行分页数据查询,注意limit方法的参数要使用page类的属性
    	$data = $this->model->field('goods_id,goods_name,goods_sn,shop_price,click_count,goods_number,is_on_sale,is_delete,is_new,is_best,is_hot')->where(['is_delete'=>0])->limit($Page->firstRow,$Page->listRows)->select();
        $catedata = $cates->getTree();
    	// var_dump($catedata);
        $this->assign('catedata',$catedata);
        $this->assign('show',$show);
        $this->assign('data',$data);
    	$this->display();
    }

    // 修改数据方法
    public function goodsmod(){
        // $modgoods = D('Goods');

        $datamod = $this->model->getTree();
        $this->assign('datam',$datamod);

        // var_dump($datamod);
        $row = $datamod->find(I('get.goods_id'));
        if(empty($row)){
            $this->redirect("{:U('Admin/Goods/goodslist')}");
        }
        $this->display('goodsedit');

    }

    // 删除数据方法
    public function goodsdel(){
        // $delgoods = D('Goods');
        $getid = I('get.goods_id');
        // var_dump($getid);
        // var_dump(where(I('get.goods_id'));exit;
        $test = $this->model->del($getid);
        // var_dump($test);

        $this->redirect('Admin/Goods/goodslist');

    }

}